//
//  ViewController.m
//  5007-2048游戏
//
//  Created by wukeng on 16/3/20.
//  Copyright © 2016年 wukeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong)  NSArray *ViewArr;
@property (nonatomic,strong) NSArray *makeLabelArr;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UIView *view4;
@property (weak, nonatomic) IBOutlet UIView *view5;
@property (weak, nonatomic) IBOutlet UIView *view6;
@property (weak, nonatomic) IBOutlet UIView *view7;
@property (weak, nonatomic) IBOutlet UIView *view8;
@property (weak, nonatomic) IBOutlet UIView *view9;
@property (weak, nonatomic) IBOutlet UIView *view10;
@property (weak, nonatomic) IBOutlet UIView *view11;
@property (weak, nonatomic) IBOutlet UIView *view12;
@property (weak, nonatomic) IBOutlet UIView *view13;
@property (weak, nonatomic) IBOutlet UIView *view14;
@property (weak, nonatomic) IBOutlet UIView *view15;
@property (weak, nonatomic) IBOutlet UIView *view16;
@property (weak, nonatomic) IBOutlet UIView *bigView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ViewArr = @[self.view1,self.view2,self.view3,self.view4,self.view5,self.view6,self.view7,self.view8,self.view9,self.view10,self.view11,self.view12,self.view13,self.view14,self.view15,self.view16];
    [self setUpFirst];
    [self setSwaipLeft];
    [self setSwaipRight];
    [self setSwaipUp];
    [self setSwaipDown];
}

//设置label
-(UILabel *)setUpLabelWithName:(NSString *)name
                WithColor:(UIColor *)color{
    CGSize size = self.view1.frame.size;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    label.text = name;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:50];
    label.backgroundColor = color;
    return label;
}
//设置向右轻扫
-(void)setSwaipLeft{
    UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveLeft)];
    left.direction = UISwipeGestureRecognizerDirectionRight;
    left.numberOfTouchesRequired = 1;
    [self.bigView addGestureRecognizer:left];
}
//向右的轻扫事件
-(void)moveLeft{
    NSLog(@"向右轻扫了");
    //1.label的移动
    //遍历4行
    for (int i = 0; i < 4; i++) {
        //遍历每行中的列
        for (int j = 4; j > 0; j--) {
            
            UIView *view1 = self.ViewArr[i*4+j-1];
            //判断遍历到的列位置的View中有没有label
            //如果没有label就执行下面的代码，遍历这个列前面的所有列，寻找有label的View，把label放到这个列的View中。
            if (view1.subviews.count == 0) {
                for (int m = j-1; m > 0; m--) {
                    
                    UIView *view2 = self.ViewArr [i*4 + m - 1];
                    if (view2.subviews.count != 0) {
                        [self.ViewArr[i*4+j-1] addSubview:view2.subviews[0]];
                        //这里一定要break；不然会把所有有label全部遍历出来加到一个View中
                        break;
                    }
                }
            }
        }
    }
    //2.在有空的位置随机添加一个Label
    [self addLabel];
}
//设置向左轻扫
-(void)setSwaipRight{
    UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight)];
    right.direction = UISwipeGestureRecognizerDirectionLeft;
    right.numberOfTouchesRequired = 1;
    [self.bigView addGestureRecognizer:right];
}
//向左的轻扫事件
-(void)moveRight{
    NSLog(@"向左轻扫了");
    //1.label的移动
    //遍历4行
    for (int i = 0; i < 4; i++) {
        //遍历每行中的列
        for (int j = 0; j < 4; j++) {
            
            UIView *view1 = self.ViewArr[i*4+j];
            //判断遍历到的列位置的View中有没有label
            //如果没有label就执行下面的代码，遍历这个列前面的所有列，寻找有label的View，把label放到这个列的View中。
            if (view1.subviews.count == 0) {
                for (int m = j + 1; m < 4; m++) {
                    
                    UIView *view2 = self.ViewArr [i*4 + m];
                    if (view2.subviews.count != 0) {
                        [self.ViewArr[i*4+j] addSubview:view2.subviews[0]];
                        //这里一定要break；不然会把所有有label全部遍历出来加到一个View中
                        break;
                    }
                }
            }
        }
    }
    [self addLabel];
}
//设置向上轻扫
-(void)setSwaipUp{
    UISwipeGestureRecognizer *up = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveUp)];
    up.direction = UISwipeGestureRecognizerDirectionUp;
    up.numberOfTouchesRequired = 1;
    [self.bigView addGestureRecognizer:up];
}
-(void)moveUp{
    NSLog(@"向上轻扫了");
    //1.label的移动
    //遍历4列
    for (int i = 0; i < 4; i++) {
        //遍历每列中的行
        for (int j = 0; j < 4; j++) {
            
            UIView *view1 = self.ViewArr[i + j*4];
            //判断遍历到的列位置的View中有没有label
            //如果没有label就执行下面的代码，遍历这个列前面的所有列，寻找有label的View，把label放到这个列的View中。
            if (view1.subviews.count == 0) {
                for (int m = j+1; m < 4; m++) {
                    
                    UIView *view2 = self.ViewArr [m*4 + i];
                    if (view2.subviews.count != 0) {
                        [self.ViewArr[i + j*4] addSubview:view2.subviews[0]];
                        //这里一定要break；不然会把所有有label全部遍历出来加到一个View中
                        break;
                    }
                }
            }
        }
    }
    [self addLabel];
}
//设置向下轻扫
-(void)setSwaipDown{
    UISwipeGestureRecognizer *down = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveDown)];
    down.direction = UISwipeGestureRecognizerDirectionDown;
    down.numberOfTouchesRequired = 1;
    [self.bigView addGestureRecognizer:down];
}
-(void)moveDown{
    NSLog(@"向下轻扫了");
    //1.label的移动
    //遍历4列
    for (int i = 0; i < 4; i++) {
        //遍历每列中的行
        for (int j = 3; j >= 0; j--) {
            
            UIView *view1 = self.ViewArr[j*4 + i];
            //判断遍历到的列位置的View中有没有label
            //如果没有label就执行下面的代码，遍历这个列前面的所有列，寻找有label的View，把label放到这个列的View中。
            if (view1.subviews.count == 0) {
                for (int m = j-1; m >= 0; m--) {
                    
                    UIView *view2 = self.ViewArr [m*4 + i];
                    if (view2.subviews.count != 0) {
                        [self.ViewArr[i + j*4] addSubview:view2.subviews[0]];
                        //这里一定要break；不然会把所有有label全部遍历出来加到一个View中
                        break;
                    }
                }
            }
        }
    }
    [self addLabel];
}

//轻扫过后添加label的方法
-(void)addLabel{
    NSMutableArray *mutArr = [NSMutableArray array];
    for (UIView *mV in self.ViewArr) {
        if (mV.subviews.count == 0) {
            [mutArr addObject:mV];
        }
    }
    int i = arc4random_uniform(2);
    if (i == 0) {
        UILabel *label = [self setUpLabelWithName:@"2" WithColor:[UIColor yellowColor]];
        [mutArr[arc4random_uniform((int)mutArr.count)] addSubview:label];
    }
    else{
        UILabel *label = [self setUpLabelWithName:@"4" WithColor:[UIColor blueColor]];
        [mutArr[arc4random_uniform((int)mutArr.count)] addSubview:label];
    }

}

//初始页面的设置
-(void)setUpFirst{
    UILabel *label2 = [self setUpLabelWithName:@"2" WithColor:[UIColor yellowColor]];
    int j = arc4random_uniform(16);
    [self.ViewArr[j] addSubview:label2];
    int i = arc4random_uniform(2);
    int m = arc4random_uniform(16);
    while (m == j) {
         m = arc4random_uniform(16);
    }
    if (i == 0) {
        UILabel *label2 = [self setUpLabelWithName:@"2" WithColor:[UIColor yellowColor]];
        [self.ViewArr[m] addSubview:label2];
    }
    else{
        UILabel *label4 = [self setUpLabelWithName:@"4" WithColor:[UIColor blueColor]];
        [self.ViewArr[m] addSubview:label4];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end









